﻿using SignInServer.SQL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WindowsFormsApp1.Project.Info;

namespace WindowsFormsApp1.SQL
{
    class SQLUserAccess
    {
        public static UserInfo UserLoginByID(int uID, string password)
        {
            Console.WriteLine(" SQL用户通过ID登录-ID" + uID + ";密码:" +password);
            SqlParameter[] p = new SqlParameter[]
            {
                   new SqlParameter("@UserID",uID),
                   new SqlParameter("@UserPassword",password)
            };
            SqlDataReader dr = SQLHelper.ExecuteReader("UserLoginByID", CommandType.StoredProcedure, p);
            UserInfo a = null;
            if (dr.Read())
            {
                a = new UserInfo(Convert.ToInt32(dr["UserID"]), Convert.ToString(dr["UserName"]));
                Console.WriteLine(" SQL用户通过ID登录-昵称" + a.userName + ";密码:" + password + ";ID:" + uID);
            }
            dr.Close();
            return a;
        }

        public static UserInfo UserLoginByName(string name, string password)
        {
            SqlParameter[] p = new SqlParameter[]
            {
                   new SqlParameter("@UserName",name),
                   new SqlParameter("@UserPassword",password)
            };
            SqlDataReader dr = SQLHelper.ExecuteReader("UserLoginByName", CommandType.StoredProcedure, p);
            UserInfo a = null;
            if (dr.Read())
            {
                a = new UserInfo(Convert.ToInt32(dr["UserID"]), Convert.ToString(dr["UserName"]));
                Console.WriteLine(" SQL用户通过昵称登录-Name" + name + ";密码:" + password + ";ID:" + a.userID);
            }
            dr.Close();
            return a;
        }

        public static int RegisterUser(string name, string password)
        {
            SqlParameter[] p = new SqlParameter[]
                     {
                            new SqlParameter("@UserName",name),
                            new SqlParameter("@UserPassword",password)
                     };
            SqlDataReader dr = SQLHelper.ExecuteReader("RegisterUser", CommandType.StoredProcedure, p);
            int uid = -1;
            if (dr.Read())
            {
                uid = Convert.ToInt32(dr["UserID"]);
                Console.WriteLine(" SQL注册用户-Name" + name + ";密码:" + password + ";ID:" + uid);
            }
            dr.Close();
            return uid;
        }

        public static UserInfo GETUserInfoByID(int uID)
        {
            SqlParameter[] p = new SqlParameter[]
            {
                   new SqlParameter("@UserID",uID)
            };
            SqlDataReader dr = SQLHelper.ExecuteReader("GetUserInfo", CommandType.StoredProcedure, p);
            UserInfo a = null;
            if (dr.Read())
            {
                a = new UserInfo(uID, Convert.ToString(dr["UserName"]));
                Console.WriteLine(" SQL获取用户-Name" + a.userName + ";ID:" + a.userID);
            }
            dr.Close();
            return a;
        }

        public static UserInfo GetJoinedActivityListByUserID(int uid)
        {
            SqlParameter[] p = new SqlParameter[]
                     {
                            new SqlParameter("UserID",uid)
                     };
            SqlDataReader dr = SQLHelper.ExecuteReader("GetJoinedActivityListByUserID", CommandType.StoredProcedure, p);
            List<int> joinedActivityList = new List<int>();
            while (dr.Read())
            {
                joinedActivityList.Add(Convert.ToInt32(dr["ActivityID"]));
            }
            dr.Close();
            UserInfo userInfo = new UserInfo();
            userInfo.SetUserjoinedInfo(uid, joinedActivityList);
            Console.WriteLine(" SQL获取用户参与活动列表-ID" + uid + ";");
            return userInfo;
        }

        public static UserInfo GetManagedActivityList(int uid)
        {
            SqlParameter[] p = new SqlParameter[]
                     {
                            new SqlParameter("UserID",uid)
                     };
            SqlDataReader dr = SQLHelper.ExecuteReader("GetManagedActivityListByUserID", CommandType.StoredProcedure, p);
            List<int> managedActivityList = new List<int>();
            while (dr.Read())
            {
                managedActivityList.Add(Convert.ToInt32(dr["ActivityID"]));
            }
            dr.Close();
            UserInfo userInfo = new UserInfo();
            userInfo.SetUsermanagedInfo(uid, managedActivityList);
            Console.WriteLine(" SQL获取用户管理活动列表-ID" + uid + ";");
            return userInfo;
        }

        public static UserInfo GetInitiatorActivityList(int activityInitiatorID)
        {
            SqlParameter[] p = new SqlParameter[]
                     {
                            new SqlParameter("ActivityInitiatorID",activityInitiatorID)
                     };
            SqlDataReader dr = SQLHelper.ExecuteReader("GetInitiatorActivityListByActivityInitiatorID", CommandType.StoredProcedure, p);
            List<int> InitiatorActivityList = new List<int>();
            while (dr.Read())
            {
                InitiatorActivityList.Add(Convert.ToInt32(dr["ActivityID"]));
            }
            dr.Close();
            UserInfo userInfo = new UserInfo();
            userInfo.SetUserInitiatorActivityList(activityInitiatorID, InitiatorActivityList);
            Console.WriteLine(" SQL获取用户发起活动列表-ID" +activityInitiatorID + ";");
            return userInfo;
        }

        public static bool SignInActivity(int activityID,int userID)
        {
            try
            {
                SqlParameter[] p = new SqlParameter[]
                     {
                            new SqlParameter("@ActivityID",activityID),
                            new SqlParameter("@UserID",userID)
                     };
                int i = SQLHelper.ExecuteNonQuery("SignInActivity", CommandType.StoredProcedure, p);
                return i > 0;
            }
            catch (Exception ex)
            {
                Console.WriteLine("用户签到时出错-activityID:"+activityID+"-userID:"+userID);
                return false;
            }
        }
    }
}
